<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/26
 * Time: 15:36
 */

namespace api\storage\controller;


use api\storage\model\PurchaseDetailModel;
use api\storage\model\ReelsModel;

class PurchaseController extends AppBaseController
{


    /**
     * @api {GET} /api/storage/purchase 查找reelId在采购单的数量
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  index
     * @apiGroup  purchase
     * @apiVersion 0.0.1
     * @apiDescription 查找reelId在采购单的数量
     *
     * @apiParam {string} reelId  reelId
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.count 物料数量
     * @apiError  Error
     **/
    public function index()
    {
        $reelId = input('reelId');
        $purchaseModel = new PurchaseDetailModel();
        $count = $purchaseModel->findCountByReelId($reelId, $this->getStorageUuid());
        if (empty($count) && $count !== 0) {
            $this->error('该物料不在系统中', ['count' => 0]);
        } else {
            $this->success('请求成功', ['count' => $count]);
        }
    }

    /**
     * @api {PUT} /api/storage/purchase/reel_count_update 更新采购单的reelId数量
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  updateReelCount
     * @apiGroup  purchase
     * @apiVersion 0.0.1
     * @apiDescription 更新采购单的reelId数量
     *
     * @apiParam {string} reelId reelId
     * @apiParam {int} count 数量
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiError  Error
     **/
    public function updateReelCount()
    {
        $reelId = input('reelId');
        $count = intval(input('count'));
        $reelModel = new ReelsModel();
        $reel = $reelModel->findReelsByIdAndUuid($reelId, $this->getStorageUuid());
        if (empty($reel)) {
            $this->error('该物料不再库中', '');
        }
        if ($reel->status == 1) {
            $this->error('该物料已在架上，无法更新', '');
        }
        $purchaseModel = new PurchaseDetailModel();
        $res = $purchaseModel->updateReelsCount($reelId, $count);
        if ($res !== false) {
            $this->success('更新成功', '');
        } else {
            $this->error('更新失败', '');
        }
    }

}